package com.wangshen.administrator.ygqq;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gc.flashview.FlashView;
import com.wangshen.administrator.ygqq.adapter.MyPagerAdapder;
import com.wangshen.administrator.ygqq.fragment.AllFragment;
import com.wangshen.administrator.ygqq.fragment.AnnounceFragment;
import com.wangshen.administrator.ygqq.fragment.CartFragment;
import com.wangshen.administrator.ygqq.fragment.HomeFragment;
import com.wangshen.administrator.ygqq.fragment.YungouFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {

    private FlashView flashView;
    @InjectView(R.id.fragment_main)
    FrameLayout fragmentMain;
    @InjectView(R.id.radio_main)
    RadioGroup radioMain;
    @InjectView(R.id.vp_main)
    ViewPager vpMain;
    private List<Fragment> list;
    private int previousIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        data();
        previousIndex = 0;
       // showFragment(0, previousIndex);
        vpMain.setAdapter(new MyPagerAdapder(getSupportFragmentManager() , list));
        vpMain.setOffscreenPageLimit(2);
        RadioButton rb = (RadioButton) radioMain.getChildAt(0);
        rb.setChecked(true);
        radioMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                int index = group.indexOfChild(rb);
                vpMain.setCurrentItem(index);

            }
        });

        vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) radioMain.getChildAt(position);
                rb.setChecked(true);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void data() {
        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new AllFragment());
        list.add(new AnnounceFragment());
        list.add(new CartFragment());
        list.add(new YungouFragment());
    }

   /* private void showFragment(int showIndex, int hideIndex) {
        Fragment showFragment = list.get(showIndex);
        Fragment hideFragment = list.get(hideIndex);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (!showFragment.isAdded()) {
            ft.add(R.id.fragment_main, showFragment);
        }
        if (showIndex == hideIndex) {
            ft.show(showFragment).commit();
        } else {
            ft.show(showFragment).hide(hideFragment).commit();
        }

    }*/
}
